package com.njtoyo.netrides.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.taxi.util.General;
import lombok.Data;
import lombok.experimental.Accessors;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by astropete on 2017/8/16.
 */
@Entity
@Table(name = "coupon")
@Data
@Accessors(chain = true)
public class Coupon {

    @Id
    @Column(name = "id", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private Long saleId;

    @Enumerated
    private Znum.CouponCtg category;

    /**
     * 适用范围：0:mix,1:realtime,2:appointment
     */
    private Znum.CouponScope scope = getScope() == null ? Znum.CouponScope.mix : getScope();

    /**
     * 适用平台： 0mix，1app，2小程序，3h5
     */
    @Enumerated
    private Znum.CouponPlatform platform = getPlatform() == null ? Znum.CouponPlatform.mix : getPlatform();

    /**
     * 来源渠道 0:system[平台发放],1:activity_team[助力]
     */
    @Enumerated
    private Znum.CouponSourceChannel sourceChannel;

    private Double digit;

    /**
     * 优惠券类型
     */
    private Znum.CouponDiscountType discountType;


    /**
     * 折扣或减免金额
     */
    private Double discountDigit;

    /**
     * 用户Id
     */
    private Long userId;

    /**
     * 运营区Id
     */
    private Long cantonId;

    /**
     * 乘客运营区Id
     */
    private Long customerRegionId;


    private String limitation;

    /**
     * 使用条件
     */
    private Znum.UsageCondition usageCondition;

    /**
     * 使用限制
     */
    private String usageLimit;

    /**
     * 开始时间
     */
    private Date startedAt;

    /**
     * 结束时间
     */
    private Date endedAt;

    /**
     * 是否已使用
     */
    @Column(name = "had_used", nullable = false, columnDefinition = "TINYINT(1)")
    private Boolean hadUsed;

    /**
     * 实际减免金额
     */
    private BigDecimal actualDiscount;

    /**
     * 使用时间（日期格式）
     */
    private Date usedAt;

    /**
     * 使用时间（时间戳格式）
     */
    private Integer usedTimestamp;

    @JsonIgnore
    private Date createdAt;

    /**
     * 创建时间（时间戳格式）
     */
    private Integer grantedTimestamp;

    /**
     * 积分商城商品Id
     */
    private Long goodsId;

    /**
     * 积分商城订单详情Id
     */
    private Long orderDetailId;

    @Transient
    private Boolean valid;

    @Transient
    private Integer expireDay;

    public enum CouponLimit {
        max,
        full
    }

    @JsonIgnore
    public Double getLimitationDigit() {
        List<String> limitationArr = Arrays.asList(limitation.split(Pattern.quote(".")));
        return General.round(Double.valueOf(limitationArr.get(1)));
    }

    public Boolean getValid() {
        return new Date().before(this.getEndedAt());
    }

    public void setValid(Boolean valid) {
        this.valid = valid;
    }

    public double value() {
        if (category.equals(Znum.CouponCtg.reduction) || category.equals(Znum.CouponCtg.fullreduction))
            return digit;
        if (category.equals(Znum.CouponCtg.discount) && null != limitation) {
            return getLimitationDigit();
        }
        return digit;
    }

    public String present() {
        if (category.equals(Znum.CouponCtg.reduction) || category.equals(Znum.CouponCtg.fullreduction)) {
            return digit.intValue() + "元券";
        }
        if (category.equals(Znum.CouponCtg.discount)) {
            return digit + "折券";
        }
        return null;
    }

    public Double getDigit() {
        if (discountDigit == null || discountDigit == 0) {
            return digit;
        } else {
            return discountDigit;
        }
    }
}